package com.sakura.mall.order.order.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.order.domain.entity.OrderEntity;
import com.sakura.mall.order.order.domain.request.AddOrderRequest;
import com.sakura.mall.order.order.domain.request.UpdateOrderRequest;
import com.sakura.mall.order.order.domain.request.OrderPageRequest;
import com.sakura.mall.order.order.domain.vo.OrderVo;
import com.sakura.mall.order.order.mapper.OrderMapper;
import com.sakura.mall.order.order.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddOrderRequest request) {
        // todo 其他校验逻辑
        OrderEntity order = request.convert(OrderEntity.class);
        this.save(order);
    }

    @Override
    public Page<OrderVo> queryPage(OrderPageRequest request) {
        IPage<OrderEntity> qp = request.toPage();
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<OrderVo> ret = qp.convert(entity -> entity.convert(OrderVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateOrderRequest request) {
        OrderEntity entity = request.convert(OrderEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

